package com.tianqu.tms.www.car.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.www.car.controller.CarInnerListTopController;
import com.tianqu.tms.www.car.viewhelper.CarInnerListTopViewHelper;

public class CarInnerListTopAction extends TmsAction{

	private static final String SUCCESS = "INNER";
	public final static String PAGENO = "pageNo";
	public final static String SORTKEY = "sortKey";
	public final static String MAXINDEX = "maxIndex";
	public final static String ASC = "asc";
	
	@SuppressWarnings("unused")
	private Logger logger = Logger.getLogger(CarInnerListTopAction.class);
	@SuppressWarnings("unchecked")
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		//viewhelper
		if (uc != null) {
			uc.clear();
			
		}
		CarInnerListTopViewHelper viewHelper = new CarInnerListTopViewHelper();
		if(request.getParameter(PAGENO) == null) {
			uc.setAttribute(PAGENO, viewHelper.getPageNo());
			uc.setAttribute(SORTKEY, "8");
			uc.setAttribute(MAXINDEX, "30");
			uc.setAttribute(ASC, "false");
		} else {
			uc.setAttribute(PAGENO, request.getParameter(PAGENO));
			uc.setAttribute(SORTKEY, request.getParameter(SORTKEY));
			uc.setAttribute(MAXINDEX, "30");
			uc.setAttribute(ASC, request.getParameter(ASC));
		}
		
		CarInnerListTopController controller = new CarInnerListTopController();
		controller.execute(uc);
		//取得待办理车辆列表
		List innerCarList = (List)uc.getAttribute(CarInnerListTopController.INNERCARLIST);
		Car car = (Car)uc.getAttribute(CarInnerListTopController.PAGING);
		//存入viewHelper
		viewHelper.setInnerCarList(innerCarList);
		
		// 一覧表示用データがViewhelperに設定する。
		viewHelper.setPageNo(Integer.toString(car.getPageNo()));
		viewHelper.setMaxIndex(Integer.toString(car.getMaxIndex()));
		viewHelper.setAsc(Boolean.toString(car.isAsc()));
		viewHelper.setSortKey(Integer.toString(car.getSortKey()));
		viewHelper.setTotalCount(Integer.toString(car.getTotalCount()));
		viewHelper.setDateFrom(Integer.toString(car.getDateFrom()));
		viewHelper.setDateTo(Integer.toString(car.getDateTo()));
		
		request.setAttribute(TmsConstants.VIEWHELP,viewHelper);
		uc.removeAttribute(CarInnerListTopController.INNERCARLIST);
		return mapping.findForward(SUCCESS);
	}

}
